package com.sanpu.kxe.business.member.view;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.member.adapter.MemberPromotAdapter;
import com.sanpu.kxe.business.member.bean.MemberPromotOutput;
import com.sanpu.kxe.business.member.contract.MemberConfig;
import com.sanpu.kxe.business.member.contract.MemberPromotListContract;
import com.sanpu.kxe.business.member.presenter.MemberPromotListPresenter;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.widget.EditTextWithDel;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Description：会员推广列表
 * Created by 薛勇军 on 2019/4/15.
 */

public class MemberPromotListActivity extends BaseTitleActivity implements MemberPromotListContract.View {

    @Bind(R.id.searchEt)
    EditTextWithDel searchEt;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.userListRv)
    RecyclerView userListRv;
    @Bind(R.id.emptyView)
    View emptyView;

    private MemberPromotAdapter mPromotAdapter;

    private MemberPromotListContract.Presenter mPresenter;

    private int nowPage;

    private boolean isSearch;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_member_promot_list;
    }

    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("分享用户列表");
        userListRv.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mPromotAdapter = new MemberPromotAdapter();
        userListRv.setAdapter(mPromotAdapter);
        mPromotAdapter.disableLoadMoreIfNotFullPage(userListRv);

        String merchantLevel = getIntent().getStringExtra(MemberConfig.INTENT_MERCHANTLEVEL);
        mPresenter = new MemberPromotListPresenter(this, merchantLevel);
        mPresenter.myUserList(nowPage);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                if (isSearch) return;
                nowPage = 0;
                mPresenter.myUserList(nowPage);
                mPromotAdapter.setEnableLoadMore(true);
            }
        });
        mPromotAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (view.getId() == R.id.callTelLl) {
                    MemberPromotOutput promotOutput = (MemberPromotOutput) adapter.getItem(position);
                    if (promotOutput != null && !TextUtils.isEmpty(promotOutput.getPhoneNumber())) {
                        Uri uri = Uri.parse("tel:" + promotOutput.getPhoneNumber());
                        Intent it = new Intent(Intent.ACTION_DIAL, uri);
                        startActivity(it);
                    }
                }
            }
        });
        mPromotAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (isSearch) return;
                mPresenter.myUserList(nowPage);
            }
        }, userListRv);
        mPromotAdapter.setPreLoadNumber(2);
    }

    @OnClick({R.id.searchIv})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.searchIv:
                String searchText = searchEt.getText().toString();
                if (TextUtils.isEmpty(searchText)) {
                    isSearch = false;
                    mPresenter.myUserList(nowPage);
                } else {
                    mPresenter.findUser(searchText);
                }
                break;
        }
    }

    @Override
    public void showPromotListView(List<MemberPromotOutput> promotOutputs, boolean isFirst) {
        if (promotOutputs != null && promotOutputs.size()>0) {
            emptyView.setVisibility(View.GONE);
            userListRv.setVisibility(View.VISIBLE);
            if (isFirst) {
                mPromotAdapter.setNewData(promotOutputs);
            } else {
                mPromotAdapter.addData(promotOutputs);
            }
            nowPage++;
        } else if (isFirst) {
            emptyView.setVisibility(View.VISIBLE);
            userListRv.setVisibility(View.GONE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                mPromotAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                mPromotAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                mPromotAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    public void showPromotView(MemberPromotOutput promotOutput) {
        List<MemberPromotOutput> memberPromotOutputs = new ArrayList<>();
        if (promotOutput != null) {
            emptyView.setVisibility(View.GONE);
            userListRv.setVisibility(View.VISIBLE);
            memberPromotOutputs.add(promotOutput);
        } else {
            emptyView.setVisibility(View.VISIBLE);
            userListRv.setVisibility(View.GONE);
        }
        mPromotAdapter.setNewData(memberPromotOutputs);
        isSearch = true;
        nowPage = 0;
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(MemberPromotListContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
